package com.agadasom.babychecker;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.TimePickerDialog;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.app.DialogFragment;


public class RegisterActivity extends Activity {
	private TextView edDate, edTime;	
	protected DateFormat dateFormatter, timeFormatter;
	protected Button btnGetDate, btnGetTime;
	protected DatePickerDialog dpd;
	protected DatePickerDialog.OnDateSetListener dpdListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int month, int day) {
			// Do something with the date chosen by the user
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//비 시각적 요소에 대한 초기화
		dateFormatter = DateFormat.getDateInstance();
		timeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT);
		
		setContentView(R.layout.activity_register);
		//시각적 요소에 대한 초기화
		edDate = (TextView)findViewById(R.id.editDate);
		edTime = (TextView)findViewById(R.id.editTime);
		btnGetDate = (Button)findViewById(R.id.btn_getdate);
		btnGetTime = (Button)findViewById(R.id.btn_gettime);
		btnGetDate.setOnClickListener(mClickListener);
		btnGetTime.setOnClickListener(mClickListener);
		
		
	}
	
	protected void onResume() {
		super.onResume();
		//사용할 요소들의 선언		
		//Calendar cal = Calendar.getInstance();		
		Date today = new Date();
		
		//사용할 요소들의 값 초기화 및 처리		
		//edDate.setText(dateFormatter.format(today));
		//edTime.setText(timeFormatter.format(today));
		setDate(today);
		setTime(today);
	}
	
	private OnClickListener mClickListener = new OnClickListener() {
	    public void onClick(View v) {
	      // do something when the button is clicked
	    	FragmentManager fm = getFragmentManager();
	    	DialogFragment newFragment;
	    	
	    	switch(v.getId()) {
	    	case(R.id.btn_getdate):
	    		//1. 최근에 사용하는 방법
	    			//dateFormatter.parse((String) edDate.getText());
	    			//dateFormatter.parse("2010/11/12");
		    		newFragment = new DatePickerFragment();
		        	newFragment.show(fm, "datePicker");
	    		//2. 무턱대고 사용하는 방법
		    		//dpd = new DatePickerDialog(v.getContext(), dpdListener, 2011, 1, 2);
		    		//dpd.show();
	    		
	    		//3. 또 다른 방법.
	    			//showDialog(1);
	    	
	    		break;
	    	case(R.id.btn_gettime):
	    			newFragment = new TimePickerFragment();
	    			newFragment.show(fm, "timePicker");
	    		break;	    	
	    	}
	    }
	};
	
	public void setDate(Date d) {
		edDate.setText(dateFormatter.format(d));
	}
	
	public CharSequence getDate() {
		return edDate.getText();
	}
	
	public void setTime(Date d) {
		edTime.setText(timeFormatter.format(d));
	}
	public CharSequence getTime() {
		return edTime.getText();
	}
	/*
	protected Dialog onCreateDialog(int id) {
		//
		return new DatePickerDialog(this, dpdListener, 2011, 1, 2);		
	}
	*/
	public static class TimePickerFragment extends DialogFragment implements
			TimePickerDialog.OnTimeSetListener {
		RegisterActivity mCaller;
		//OnDataSetListener mCallback;
		//public interface OnDataSetListener {
		//	public void onDataSet(Date d);
		//}
		@Override
		public void onAttach(Activity activity) {
			super.onAttach(activity);
			mCaller = (RegisterActivity)activity;	        
		}
		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			// Use the current time as the default values for the picker
			final Calendar c = Calendar.getInstance();
			int hour = c.get(Calendar.HOUR_OF_DAY);
			int minute = c.get(Calendar.MINUTE);
			
			//Activity act = getActivity();
			//act 를 mCaller 로 변경함.
			//TextView edTime = (TextView)mCaller.findViewById(R.id.editTime);
			//TextView edDate = (TextView)mCaller.findViewById(R.id.editDate);
			CharSequence txtTime,txtDate;
			txtTime = ((RegisterActivity) mCaller).getTime();
			txtDate = ((RegisterActivity) mCaller).getDate();
			//DateFormat timeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT);
			DateFormat dateTimeFormatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
			StringBuffer sb = new StringBuffer();
			//sb.append(edDate.getText());
			sb.append(txtDate);
			sb.append(" ");
			//sb.append(edTime.getText());
			sb.append(txtTime);
			try {
				Date dt = dateTimeFormatter.parse( sb.toString() );
				c.setTime(dt);
				hour = c.get(Calendar.HOUR_OF_DAY);
				minute = c.get(Calendar.MINUTE);				
				
			} catch (ParseException e) {				
				e.printStackTrace();
			}
			
			// Create a new instance of TimePickerDialog and return it
			return new TimePickerDialog(getActivity(), this, hour, minute,
					false);
		}

		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			// Do something with the time chosen by the user
			final Calendar cal = Calendar.getInstance();
						
			//Activity act = getActivity();
			//act 를 mCaller 로 변경함
			/* 
			// mCaller 활용함.
			 * TextView edTime = (TextView)mCaller.findViewById(R.id.editTime);
			 * TextView edDate = (TextView)mCaller.findViewById(R.id.editDate);			
			 */
			
			DateFormat dateTimeFormatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
			StringBuffer sb = new StringBuffer();
			//sb.append(edDate.getText());
			sb.append(mCaller.getDate());
			sb.append(" ");
			//sb.append(edTime.getText());
			sb.append(mCaller.getTime());
			try {
				Date dt = dateTimeFormatter.parse( sb.toString() );
				cal.setTime(dt);
				cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
				cal.set(Calendar.MINUTE, minute);
				//DateFormat timeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT);
				//edTime.setText(timeFormatter.format(cal.getTime()));
				mCaller.setTime(cal.getTime());
			} catch (ParseException e) {				
				e.printStackTrace();
			}					
			
		}
	}
	public static class DatePickerFragment extends DialogFragment implements
			DatePickerDialog.OnDateSetListener {
		RegisterActivity mCaller;
		@Override
		public void onAttach(Activity activity) {
			super.onAttach(activity);
			mCaller = (RegisterActivity) activity;	        
		}
		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			// Use the current date as the default date in the picker
			final Calendar c = Calendar.getInstance();
			int year = c.get(Calendar.YEAR);
			int month = c.get(Calendar.MONTH);
			int day = c.get(Calendar.DAY_OF_MONTH);
			
			//c.set(year,month,day);
						
			TextView edDate = (TextView)mCaller.findViewById(R.id.editDate);
			DateFormat dateFormatter = DateFormat.getDateInstance();
			Date dt;
			try {
				dt = dateFormatter.parse(edDate.getText().toString());
				c.setTime(dt);
				year = c.get(Calendar.YEAR);
				month = c.get(Calendar.MONTH);
				day = c.get(Calendar.DAY_OF_MONTH);
			} catch (ParseException e) {				
				e.printStackTrace();
			}			
			// Create a new instance of DatePickerDialog and return it
			return new DatePickerDialog(getActivity(), this, year, month, day);
		}

		public void onDateSet(DatePicker view, int year, int month, int day) {
			// Do something with the date chosen by the user
			final Calendar cal = Calendar.getInstance();
			cal.set(year,month,day);
						
			/* mCaller 활용함.
			TextView edDate = (TextView)mCaller.findViewById(R.id.editDate);
			DateFormat dateFormatter = DateFormat.getDateInstance();
			edDate.setText(dateFormatter.format(cal.getTime()));
			*/
			mCaller.setDate(cal.getTime());
		}
	}
	
/* Manifest 의 Target SDK 를 13 이상으로 했을 때 주석 제거
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_register, menu);
		return true;
	}
*/
}
